<input type="hidden" id="manage_group_status" value="<?php echo $this->session->flashdata('manage_group_status'); ?>" />
<script type="text/javascript">
  $(document).ready(function() {
    var manage_group_status = $.trim($('#manage_group_status').val());
    if (manage_group_status == 1) {
      toastr.success('Group members added successfully !');
      return false;
    } else if (manage_group_status == 2) {
      toastr.error('Error occured. Try again !');
      return false;
    } else if (manage_group_status == 3) {
      toastr.success('Group members removed successfully !');
      return false;
    } else if (manage_group_status == 4) {
      toastr.success('Group members updated successfully !');
      return false;
    }
  });
</script>

<div class="page" style="min-height: 500px;">
  <div class="page-container">
    <div class="container">
      <div class="row">
        <div class="span3">
          <h4 class="header">Manage Groups</h4>
          <div class="sidebar">
            <ul class="col-nav span3">
              <li><a href="<?php echo base_url('groups_manage/group/'); ?>"><i class="pull-right icon-th-large"></i>Manage Groups</a></li>
              <li><a style="background-color: #EEE;" href="<?php echo base_url('groups_manage/group_member/'); ?>"><i class="pull-right icon-user"></i>Manage Group Members</a></li>
              <li><a href="<?php echo base_url('groups_manage/group_event/'); ?>"><i class="pull-right icon-th-list"></i>Manage Group Events</a></li>
              <li><a href="<?php echo base_url('groups_manage/group_event_result/'); ?>"><i class="pull-right icon-signal"></i>Manage Group Event Results</a></li>
              <li><a href="<?php echo base_url('groups_manage/api_apps/'); ?>"><i class="pull-right icon-fire"></i>API Apps</a></li>
            </ul>
          </div>
        </div>
        <div class="span9">
          <div class="btn-group pull-right">
            <a title="Add group members" href="#add_group_member" data-toggle="modal" class="btn">Add</a>
            <a title="Remove group members" href="#remove_group_member" data-toggle="modal" class="btn">Remove</a>
            <a title="<?php echo 'Can accessed by : @' . @implode(', @', $this->comitte_members); ?>" href="<?php echo base_url('groups_manage'); ?>" class="btn">Manage group</a>
          </div>
          <h4 class="header">Group members [<?php echo NOW_YEAR; ?>]</h4>
          <?php if (!empty($groups)) { ?>
            <table class="table table-striped table-bordered">
              <thead>
                <tr>
                  <th width="5%">#</th>
                  <th width="35%">Group name</th>
                  <th width="50%">Group members</th>
                  <th width="5%">Total</th>
                  <th width="5%">Action</th>
                </tr>
              </thead>
              <tbody>
                <?php $count = 1; ?>
                <?php foreach ($groups as $group) { ?>
                  <tr>
                    <td><?php echo $count; ?></td>
                    <td><?php echo $group['group_name']; ?></td>
                    <td>
                      <?php if (!empty($group['group_members'])) { ?>
                        <?php foreach ($group['group_members'] as $group_member) { ?>
                          <a style="text-decoration: none; cursor: pointer;" href="<?php echo base_url('profile/' . $group_member['user_name']); ?>"><?php echo '@' . $group_member['user_name'] . ', '; ?></a>
                        <?php } ?>
                      <?php } ?>
                    </td>
                    <td><?php echo count($group['group_members']); ?></td>
                    <td>
                      <a href="#update_group_members_<?php echo $group['group_id']; ?>" data-toggle="modal" title="Update group" style="text-decoration: none; color: skyblue; cursor: pointer;"><i class="icon-large icon-pencil"></i></a>
                    </td>
                  </tr>
                  <?php $count++; ?>
                <?php } ?>
              </tbody>
            </table>
          <?php } else { ?>
            <div class="alert alert-danger">No group members !</div>
          <?php } ?>
        </div>
      </div>
    </div>
  </div>
</div>

<div id="add_group_member" class="modal hide fade">
  <div class="modal-header">
    <button type="button" data-dismiss="modal" aria-hidden="true" class="close">&times;</button>
    <h3>Add group members</h3>
  </div>
  <form class="form-horizontal" method="post" action="<?php echo base_url('groups_manage/group_member_add'); ?>">
    <input type="hidden" name="csrf_token" value="<?php echo set_csrf_token(); ?>"/>
    <div class="modal-body">
      <?php if (!empty($groups)) { ?>
        <div class="control-group">
          Select Group
          <select name="add_members_group_id" style="width: 100%;" required="required">
            <?php if (!empty($groups)) { ?>
              <?php foreach ($groups as $group) { ?>
                <option value="<?php echo $group['group_id']; ?>"><?php echo $group['group_name']; ?></option>
              <?php } ?>
            <?php } ?>
          </select>
        </div>
        <?php if (!empty($ungrouped_members)) { ?>
          Select Members to add (<b>Showing ungrouped users</b>)<br/>
          <table class="table table-striped table-bordered">
            <thead>
              <tr>
                <th width="5%">#</th>
                <th width="45%">Username</th>
                <th width="50%">Name</th>
              </tr>
            </thead>
            <tbody>
              <?php $count = 1; ?>
              <?php foreach ($ungrouped_members as $ug_member) { ?>
                <tr>
                  <td><?php echo $count; ?></td>
                  <td><input value="<?php echo $ug_member['user_id']; ?>" name="add_members_user_id[]" type="checkbox" placeholder="Group name (25)"/>&nbsp;<?php echo '@' . $ug_member['user_name']; ?></td>
                  <td><?php echo $ug_member['user_display_name']; ?></td>
                </tr>
                <?php $count++; ?>
              <?php } ?>
            </tbody>
          </table>
        <?php } else { ?>
          <div class="alert alert-danger">No ungrouped users !</div>
        <?php } ?>
      <?php } else { ?>
        <div class="alert alert-danger">Please add a group first !</div>
      <?php } ?>
    </div>
    <div class="modal-footer">
      <button data-dismiss="modal" class="btn">Close</button>
      <button type="submit" class="btn btn-primary">Save</button>
    </div>
  </form>
</div>

<div id="remove_group_member" class="modal hide fade">
  <div class="modal-header">
    <button type="button" data-dismiss="modal" aria-hidden="true" class="close">&times;</button>
    <h3>Remove group members</h3>
  </div>
  <form class="form-horizontal" method="post" action="<?php echo base_url('groups_manage/group_member_remove'); ?>">
    <input type="hidden" name="csrf_token" value="<?php echo set_csrf_token(); ?>"/>
    <div class="modal-body">
      <?php if (!empty($grouped_members)) { ?>
        Select members to remove (<b>Showing grouped users</b>)
        <table class="table table-striped table-bordered">
          <thead>
            <tr>
              <th width="5%">#</th>
              <th width="45%">Username</th>
              <th width="50%">Name</th>
            </tr>
          </thead>
          <tbody>
            <?php $count = 1; ?>
            <?php foreach ($grouped_members as $g_member) { ?>
              <tr>
                <td><?php echo $count; ?></td>
                <td><input value="<?php echo $g_member['user_id']; ?>" name="remove_members_user_id[]" type="checkbox" placeholder="Group name (25)"/>&nbsp;<?php echo '@' . $g_member['user_name']; ?></td>
                <td><?php echo $g_member['user_display_name']; ?></td>
              </tr>
              <?php $count++; ?>
            <?php } ?>
          </tbody>
        </table>
      <?php } else { ?>
        <div class="alert alert-danger">No group members !</div>
      <?php } ?>
    </div>
    <div class="modal-footer">
      <button data-dismiss="modal" class="btn">Close</button>
      <button type="submit" class="btn btn-primary">Save</button>
    </div>
  </form>
</div>

<?php foreach ($groups as $group) { ?>
  <div id="update_group_members_<?php echo $group['group_id']; ?>" class="modal hide fade">
    <div class="modal-header">
      <button type="button" data-dismiss="modal" aria-hidden="true" class="close">&times;</button>
      <h3>Update group</h3>
    </div>
    <form class="form-horizontal" method="post" action="<?php echo base_url('groups_manage/group_member_update/' . $group['group_id']); ?>">
      <input type="hidden" name="csrf_token" value="<?php echo set_csrf_token(); ?>"/>
      <input type="hidden" name="group_id" value="<?php echo $group['group_id']; ?>"/>
      <div class="modal-body">

        <div class="control-group">
          <label class="control-label">Capitan</label>
          <div class="controls">
            <select name="group_capitan_user_id">
              <option value="0">--</option>
              <?php if (!empty($group['group_members'])) { ?>
                <?php foreach ($group['group_members'] as $group_member) { ?>
                  <option <?php if ($group['group_c_user_id'] == $group_member['user_id']) { ?> selected="selected" <?php } ?> value="<?php echo $group_member['user_id']; ?>"><?php echo '@' . $group_member['user_name']; ?></option>
                <?php } ?>
              <?php } ?>
            </select>
          </div>
        </div>
        <div class="control-group">
          <label class="control-label">Vice Capitan</label>
          <div class="controls">
            <select name="group_vice_capitan_user_id">
              <option value="0">--</option>
              <?php if (!empty($group['group_members'])) { ?>
                <?php foreach ($group['group_members'] as $group_member) { ?>
                  <option <?php if ($group['group_vc_user_id'] == $group_member['user_id']) { ?> selected="selected" <?php } ?> value="<?php echo $group_member['user_id']; ?>"><?php echo '@' . $group_member['user_name']; ?></option>
                <?php } ?>
              <?php } ?>
            </select>
          </div>
        </div>
        <div class="control-group">
          <label class="control-label">Comitte Member</label>
          <div class="controls">
            <select name="group_comitte_member_user_id">
              <option value="0">--</option>
              <?php if (!empty($group['group_members'])) { ?>
                <?php foreach ($group['group_members'] as $group_member) { ?>
                  <option <?php if ($group['group_cm_user_id'] == $group_member['user_id']) { ?> selected="selected" <?php } ?> value="<?php echo $group_member['user_id']; ?>"><?php echo '@' . $group_member['user_name']; ?></option>
                <?php } ?>
              <?php } ?>
            </select>
          </div>
        </div>
        <?php if (empty($group['group_members'])) { ?>
          <div class="alert alert-danger">Please add group members first !</div>
        <?php } ?>
      </div>
      <div class="modal-footer">
        <button data-dismiss="modal" class="btn">Close</button>
        <button type="submit" class="btn btn-primary">Save</button>
      </div>
    </form>
  </div>
<?php } ?>
